package com.xqboss.common.utils;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @author liuyaowei
 * @date 2023-02-09 15:43
 * @desc
 */
public class NumberUtils {
 
    private static boolean isMatch(String regex, String orginal){
        if (orginal == null || orginal.trim().equals("")) {
            return false;
        }
        Pattern pattern = Pattern.compile(regex);
        Matcher isNum = pattern.matcher(orginal);
        return isNum.matches();
    }
 
    /**
     *  校验是否是正整数
     * @param orginal
     *
     * Assert.assertTrue(NumberValidationUtils.isPositiveInteger("1"));
     * Assert.assertTrue(NumberValidationUtils.isPositiveInteger("+12"));
     * Assert.assertTrue(NumberValidationUtils.isPositiveInteger("87653521123567"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0.1"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveInteger("0123"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveInteger("-1"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveInteger("-0.1"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveInteger("ab"));
     * @return
     */
    public static boolean isPositiveInteger(String orginal) {
        return isMatch("^\\+{0,1}[1-9]\\d*", orginal);
    }

 
    /**
     * Assert.assertTrue(NumberValidationUtils.isNegativeInteger("-1"));
     * Assert.assertTrue(NumberValidationUtils.isNegativeInteger("-87653521123567"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0.1"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeInteger("0123"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeInteger("1"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeInteger("-0.1"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeInteger("ab"));
     * @param orginal
     * @return
     */
    public static boolean isNegativeInteger(String orginal) {
        return isMatch("^-[1-9]\\d*", orginal);
    }
 
    /**
     * Assert.assertTrue(NumberValidationUtils.isWholeNumber("0"));
     * Assert.assertTrue(NumberValidationUtils.isWholeNumber("1"));
     * Assert.assertTrue(NumberValidationUtils.isWholeNumber("+12"));
     * Assert.assertTrue(NumberValidationUtils.isWholeNumber("8673434231"));
     * Assert.assertTrue(NumberValidationUtils.isWholeNumber("-282464334"));
     * Assert.assertFalse(NumberValidationUtils.isWholeNumber("0123"));
     * Assert.assertFalse(NumberValidationUtils.isWholeNumber("0.1"));
     * Assert.assertFalse(NumberValidationUtils.isWholeNumber("-0.1"));
     * Assert.assertFalse(NumberValidationUtils.isWholeNumber("ab"));
     * @param orginal
     * @return
     */
    public static boolean isWholeNumber(String orginal) {
        return isMatch("[+-]{0,1}0", orginal) || isPositiveInteger(orginal) || isNegativeInteger(orginal);
    }
 
    /**
     * Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("0.132213"));
     * Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("30.00"));
     * Assert.assertTrue(NumberValidationUtils.isDecimal("0."));
     * Assert.assertTrue(NumberValidationUtils.isPositiveDecimal("+12.0"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("0123"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("1"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("0.0"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("ab"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("-1"));
     * Assert.assertFalse(NumberValidationUtils.isPositiveDecimal("-0.1"));
     * @param orginal
     * @return
     */
    public static boolean isPositiveDecimal(String orginal){
        return isMatch("\\+{0,1}[0]\\.[1-9]*|\\+{0,1}[1-9]\\d*\\.\\d*", orginal);
    }
 
    /**
     * Assert.assertTrue(NumberValidationUtils.isNegativeDecimal("-0.132213"));
     * Assert.assertTrue(NumberValidationUtils.isNegativeDecimal("-1.0"));
     * Assert.assertTrue(NumberValidationUtils.isDecimal("-0."));
     * Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("1"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0123"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0.0"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("ab"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("-1"));
     * Assert.assertFalse(NumberValidationUtils.isNegativeDecimal("0.1"));
     * @param orginal
     * @return
     */
    public static boolean isNegativeDecimal(String orginal){
        return isMatch("^-[0]\\.[1-9]*|^-[1-9]\\d*\\.\\d*", orginal);
    }
 
    /**
     * Assert.assertTrue(NumberValidationUtils.isDecimal("0.00"));
     * Assert.assertTrue(NumberValidationUtils.isDecimal("+0.0"));
     * Assert.assertTrue(NumberValidationUtils.isDecimal("-0.132213"));
     * Assert.assertTrue(NumberValidationUtils.isDecimal("0."));
     * Assert.assertFalse(NumberValidationUtils.isDecimal("1"));
     * Assert.assertFalse(NumberValidationUtils.isDecimal("0123"));
     * Assert.assertFalse(NumberValidationUtils.isDecimal("0"));
     * Assert.assertFalse(NumberValidationUtils.isDecimal("ab"));
     * Assert.assertFalse(NumberValidationUtils.isDecimal("-1"));
     * @param orginal
     * @return
     */
    public static boolean isDecimal(String orginal){
        return isMatch("[-+]{0,1}\\d+\\.\\d*|[-+]{0,1}\\d*\\.\\d+", orginal);
    }
 
    /**
     * Assert.assertTrue(NumberValidationUtils.isRealNumber("-0.234"));
     * Assert.assertTrue(NumberValidationUtils.isRealNumber("0.0"));
     * Assert.assertTrue(NumberValidationUtils.isRealNumber("1"));
     * Assert.assertTrue(NumberValidationUtils.isRealNumber("+0.14"));
     * Assert.assertTrue(NumberValidationUtils.isRealNumber("-1"));
     * Assert.assertTrue(NumberValidationUtils.isRealNumber("0.0"));
     * Assert.assertFalse(NumberValidationUtils.isRealNumber("00.13"));
     * Assert.assertFalse(NumberValidationUtils.isRealNumber("ab"));
     * @param orginal
     * @return
     */
    public static boolean isRealNumber(String orginal){
        return isWholeNumber(orginal) || isDecimal(orginal);
    }
 
}